package com.qianfeng.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qianfeng.biz.IGoodsTypeBiz;
import com.qianfeng.biz.impl.GoodsTypeBizImpl;
import com.qianfeng.commons.WebUtils;
import com.qianfeng.dto.PageBean;
import com.qianfeng.entity.GoodType;

/**
 * Servlet implementation class GoodsTypeServlet
 */
public class GoodsTypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IGoodsTypeBiz gtBiz = new GoodsTypeBizImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	doPost(request,response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	String action = request.getParameter("action");
	PrintWriter out = response.getWriter();
	if("queryAllType".equals(action)){
		//获取当前页;
		String currentPge = request.getParameter("currentPage");
		int pagesize=100;
		PageBean page = gtBiz.queryAllfenyeType(currentPge,pagesize);
		request.getSession().setAttribute("gtList", page.getList());
		request.getSession().setAttribute("page", page);
		response.sendRedirect("goodstype/goodstype.jsp");
	}else if("toUpdate".equals(action)){
		int typeid = WebUtils.StringToInt(request.getParameter("id"), -1);
		GoodType gt = gtBiz.getGoodTypeById(typeid);
		request.getSession().setAttribute("goodType", gt);
		response.sendRedirect("goodstype/goodstypeupdate.jsp");
		
	}else if("update".equals(action)){
		String typename = request.getParameter("typename");
		int typeid = WebUtils.StringToInt(request.getParameter("typeid"), -1);
		int fatherid = WebUtils.StringToInt(request.getParameter("fatherid"), -1);
		GoodType goodType=new GoodType();
		goodType.setId(typeid);
		goodType.setGtype_parentid(fatherid);
		goodType.setGtype_name(typename);
		int result = gtBiz.updateGoodTypeInfo(goodType);
		if(result>0){
			out.write("<script>alert('修改类别信息成功');location.href='GoodsTypeServlet?action=queryAllType&currentPage=1'</script>");
		}else{
			out.write("<script>alert('修改类别信息失败');location.href='GoodsTypeServlet?action=queryAllType&currentPage=1'</script>");
		}
	}
	else if("delete".equals(action)){
		int typeid = WebUtils.StringToInt(request.getParameter("id"), -1);
		
		int result = gtBiz.deleteById(typeid);
		if(result>0){
			out.write("<script>alert('删除类别信息成功');location.href='GoodsTypeServlet?action=queryAllType&currentPage=1'</script>");
		}else{
			out.write("<script>alert('删除类别信息失败');location.href='GoodsTypeServlet?action=queryAllType&currentPage=1'</script>");
		}
	}
	}

}
